//
//  HttpClient.m
//  TrainTree
//
//  Created by wanghui on 2017/8/25.
//  Copyright © 2017年 YouYeShu. All rights reserved.
//

#import "HttpClient.h"

static NSString *const TestURL = @"http://rapapi.org/mockjsdata/25040/api/";
static NSString *const OnLineURL = @"http://admin.yoitree.com.cn/api/";
                                   

@implementation HttpClient
+ (instancetype)shareInstanceClient
{
    static HttpClient *shareInstanceClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareInstanceClient = [[HttpClient alloc]initWithBaseURL:[NSURL URLWithString:[self choiceUrl]]];
        shareInstanceClient.securityPolicy =  [self customSecurityPolicy];
        shareInstanceClient.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html",@"text/plain", nil];
//        [shareInstanceClient.requestSerializer setValue:[Session shareSession].token forHTTPHeaderField:@"token"];
//        // 视频播放加这两个头
//        [shareInstanceClient.requestSerializer willChangeValueForKey:@"timeoutInterval"];
        shareInstanceClient.requestSerializer.timeoutInterval = 10.0f;
//        [shareInstanceClient.requestSerializer didChangeValueForKey:@"timeoutInterval"];
    });
 
        [shareInstanceClient.requestSerializer setValue:[Session shareSession].token forHTTPHeaderField:@"token"];

    return shareInstanceClient;
}

+ (NSString *)choiceUrl
{
    return OnLineURL;
}

- (id)initWithBaseURL:(NSURL *)url {
    self = [super initWithBaseURL:url];

    if (self) {
//        NSString *signature = [Session shareSession].signature;
//        NSString *header =
//        [NSString stringWithFormat:@"signature=%@;uin=%@", signature, [Session shareSession].uin];
//        [self.requestSerializer setValue:header forHTTPHeaderField:@"Cookie"];
    }
    return self;
}

+ (AFSecurityPolicy *)customSecurityPolicy
{
    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy defaultPolicy];
    securityPolicy.allowInvalidCertificates = YES;
    securityPolicy.validatesDomainName = NO;
    return securityPolicy;
}

#pragma  mark - 以后token失效在此处加入弹出登录界面
- (void)PostReqeustParameter:(RequestParam *)param
                    response:(ResponseData)responseBlock {

    NSString *url =  [[NSURL URLWithString:param.requestPath relativeToURL:self.baseURL] absoluteString];

//    [[HttpClient shareInstanceClient] POST:url parameters:param.generationParam progress:^(NSProgress * _Nonnull uploadProgress) {
    [self POST:url parameters:param.generationParam progress:^(NSProgress * _Nonnull uploadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

        if([responseObject[@"c"]  intValue] == 20002) {
            [Login presentLoginPage];
        }else{
            responseBlock(responseObject,nil,task);
        }

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        responseBlock(nil, error, task);
    }];

}

- (void)GetReqeustParameter:(RequestParam *)param
                  response:(ResponseData)responseBlock {

    NSString *url =  [[NSURL URLWithString:param.requestPath relativeToURL:self.baseURL] absoluteString];

    [self GET:url parameters:param.generationParam progress:^(NSProgress * _Nonnull downloadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if([responseObject[@"c"]  intValue] == 20002) {
            [Login presentLoginPage];
        }else{
            responseBlock(responseObject,nil,task);
        }


    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        responseBlock(nil,error,task);
    }];

}
 

@end
